<template>
	<div class="system-menu-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="menuDialogFormRef" :model="state.ruleForm" size="default" label-width="80px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="上级">
							<el-cascader
								:options="state.classData"
								:props="{ checkStrictly: true, value: 'id', label: 'name' }"
								placeholder="请选择上级"
								clearable
								class="w100"
								v-model="state.ruleForm.pid"
							>
								<template #default="{ node, data }">
									<span>{{ data.name }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="名称">
							<el-input v-model="state.ruleForm.name" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="简称">
							<el-input v-model="state.ruleForm.subname" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序">
							<el-input-number v-model="state.ruleForm.sort" controls-position="right" placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="icon"  prop="icon">
							<el-upload
								class="avatar-uploader"
								:show-file-list="false"
								:action="state.action"
								:on-change="handlePdfChange"
								@change="handleChange"
								multiple>
								<img v-if="state.ruleForm.icon" :src="state.ruleForm.icon" class="avatar" />
								<el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
							</el-upload>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemMenuDialog">
import { reactive, ref } from 'vue';
import { useCategoryApi } from '/@/api/category';
import { ElMessage } from 'element-plus';
import { Plus } from '@element-plus/icons-vue';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);


// 定义变量内容
const menuDialogFormRef = ref();
const state = reactive({
	// 参数请参考 `/src/router/route.ts` 中的 `dynamicRoutes` 路由菜单格式
	ruleForm: {
		pid: [], // 上级菜单
		name:'',
		sort: 0, // 菜单排序
		id:'',
		subname:'',
		type:'',
		icon:''
	},
	classData: [], // 上级数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	action: import.meta.env.VITE_API_URL+'/common/upload',
});
const handlePdfChange = (file: any) => {
	state.ruleForm.icon = file;
}
const handleChange = (file: any) => {
	if (file.status === 'success') {
		state.ruleForm.icon = file.response.data.path;
	} else {
		state.ruleForm.icon = '';
	}
};
// 获取 pinia 中的路由
const getMenuData = async() => {
	try{
		const response = await useCategoryApi().getList();
		state.classData = response.data;
	}catch(error){
		// console.error('获取数据失败:', error);
	}
};
// 打开弹窗
const openDialog = (type: string, row?: any) => {
	getMenuData();
	if (type === 'edit') {
		// 模拟数据，实际请走接口
		state.ruleForm.pid = row.pid;
		state.ruleForm.name = row.name;
		state.ruleForm.id   = row.id;
		state.ruleForm.sort = row.sort;
		state.ruleForm.subname = row.subname;
		state.ruleForm.icon = row.icon;
		state.dialog.title = '修改';
		state.dialog.submitTxt = '修 改';
	} else if(type === 'add_menu') {
		state.dialog.title = '新增类别';
		state.dialog.submitTxt = '新 增';
		state.ruleForm.pid = row.id;
		state.ruleForm.icon = '';
	}else{
		state.dialog.title = '新增菜单';
		state.dialog.submitTxt = '新 增';
		state.ruleForm.icon = '';
	}
	state.ruleForm.type = type;
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = async () => {
	//将数据传递到后端
	if(state.ruleForm.type==='edit'){
		await useCategoryApi().postUpdate(state.ruleForm);
	}else{
		await useCategoryApi().postAdd(state.ruleForm);
	}
	closeDialog(); // 关闭弹窗
	ElMessage.success('操作成功');
	emit('refresh');
	// if (state.dialog.type === 'add') { }
	// setBackEndControlRefreshRoutes() // 刷新菜单，未进行后端接口测试
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>
<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>